<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2018/1/5
 * Time: 15:56
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;

class StatController extends BaseController
{
    public function __construct()
    {
        //无需进行权限检测的功能
        \Org\Auth\AuthUtil::setNoAuthFuncList([
            '/admin/stat/yzStatCustomerOrderList',
            '/admin/stat/yzStatOrderDetail',
            '/admin/stat/staffStatOrderList',
            '/admin/stat/staffStatExport',
            '/admin/stat/manageStaffStatExport',
            '/admin/stat/manageExportOrderList',
            '/admin/stat/exportStatCustomerData',
        ]);
        parent::__construct();
    }
    /**
     * 业支客户统计
     */
    public function yzStatCustomerList() {
        $type = trim(I('get.type'));
        $company_name = trim(I('get.company_name'));
        $admin_name = trim(I('get.admin_name'));
        $product_name = trim(I('get.product_name'));
        $department_id = trim(I('get.department_id'));
        $page = intval(I('get.page',1));
        $search_time = I("get.search_time");
        $start = '';
        $end = '';
        if($search_time){
            $all_time = explode('-',$search_time);
            $start = strtotime($all_time[0]);
            $end = strtotime($all_time[1]." 23:59:59");
        }
        $page_size = 15;

        $data = array();
        isset($page) && $data['page'] = $page;
        isset($page_size) && $data['page_size'] = $page_size;
        $company_name && $data['company_name'] = $company_name;
        $admin_name && $data['admin_name'] = $admin_name;
        $department_id && $data['department_id'] = $department_id;
        $product_name && $data['product_name'] = $product_name;
        $type && $data['type'] = $type;
        $start && $data['start'] = $start;
        $end && $data['end'] = $end;

        $result = D('Stat','Logic')->yzStatCustomerList($data);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $customerList = $result['dataList'];

        //生成分页
        $url = "/admin/stat/yzStatCustomerList?search_time={$search_time}&admin_name={$admin_name}&department_id={$department_id}&company_name={$company_name}&type={$type}";
        $page_html = create_page($url, $total, $page, $page_size);
        //部门列表
        $departmentList = D('Resource/User','Service')->getDepartmentList([]);

        //客户类别
        $typeList = C('CUSTOMER_TYPE');

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('type', $type);
        $this->assign('search_time', $search_time);
        $this->assign('department_id', $department_id);
        $this->assign('company_name', $company_name);
        $this->assign('admin_name', $admin_name);
        $this->assign('product_name', $product_name);
        $this->assign('customerList', $customerList);
        $this->assign('departmentList', $departmentList);
        $this->assign('typeList', $typeList);

        $this->loadFrame('yzStatCustomerList');
    }

    /**
     * 客户订单列表
     */
    public function yzStatCustomerOrderList() {
        $company_name = trim(I('get.company_name'));
        $page = intval(I('get.page',1));
        $page_size = 15;

        $data = array();
        isset($page) && $data['page'] = $page;
        isset($page_size) && $data['page_size'] = $page_size;
        $company_name && $data['company_name'] = $company_name;

        $result = D('Stat','Logic')->yzStatCustomerOrderList($data);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];
        //生成分页
        $url = "/admin/stat/yzStatCustomerOrderList?company_name={$company_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('orderList', $orderList);

        $this->loadFrame('yzStatCustomerOrderList');
    }
    /**
     * 订单详情
     */
    public function yzStatOrderDetail(){
        $order_id = I("get.order_id");
        if(!$order_id){
            header('Location:/order/salesmanOrderList');
            exit();
        }
        $data = [];
        $data['id'] = $order_id;
        $orderInfo = D('Order','Logic')->getOrderDetail($data);

        $this->assign('order_id',$order_id);
        $this->assign('orderInfo',$orderInfo);
        $this->loadFrame('yzStatOrderDetail');
    }

    /**
     * 业务员（统计）
     */
    public function staffStatList(){
        $name = I('get.name');
        $group_id = I('get.group_id');
        $department_id = I('get.department_id');
        $data = array();
        $name && $data['name'] = $name;
        $group_id && $data['group_id'] = $group_id;
        $department_id && $data['department_id'] = $department_id;

        $userList = D('Stat','Logic')->staffStatList($data);
        //用户组
        $groupList = D('Resource/User','Service')->getUserGroupList([]);
        //部门
        $departmentList = D('Resource/User','Service')->getDepartmentList([]);

        $this->assign('name', $name);
        $this->assign('department_id', $department_id);
        $this->assign('group_id', $group_id);
        $this->assign('userList', $userList);
        $this->assign('groupList', $groupList);
        $this->assign('departmentList', $departmentList);

        $this->loadFrame('staffStatList');
    }

    /**
     * 数据导出
     */
    public function staffStatExport(){
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $name = I('get.name');
        $group_id = I('get.group_id');
        $department_id = I('get.department_id');
        $data = array();
        $name && $data['name'] = $name;
        $group_id && $data['group_id'] = $group_id;
        $department_id && $data['department_id'] = $department_id;

        $userList = D('Stat','Logic')->staffStatList($data);
        $export_data = [];
        $export_data[] = array('角色','部门','姓名','总客户数','总订单数','成单数','进行中','销售额','成本金额','其他费用','');
        //用户组
        foreach ($userList as $key=>$user){
            $export_data[] = array(
                'user_group_name' => $user['user_group_name'],
                'department_name' => $user['department_name'],
                'name' => $user['name'],
                'all_customer' => $user['all_customer'],
                'all_order' => $user['all_order'],
                'over_order' => $user['over_order'],
                'going_order' => $user['going_order'],
                'total_amount' => $user['total_amount'],
                'total_base_price' => $user['total_base_price'],
                'total_other_price' => $user['total_other_price']
            );
        }
        $xls = new \Org\Util\PhpExcel;
        $xls->addArray($export_data);
        $xls->generateXML("order_" . date("Y-m-d H:i:s"));
    }
    /**
     * 业务员订单列表（统计）
     */
    public function staffStatOrderList() {
        $admin_id = trim(I('get.admin_id'));
        $page = intval(I('get.page',1));
        $page_size = 15;
        if(!$admin_id){
            header('Location:/admin/stat/staffStatList');
            exit();
        }
        $userInfo = D('Resource/User','Service')->getUserInfo(['id'=>$admin_id]);
        $data = array();
        isset($page) && $data['page'] = $page;
        isset($page_size) && $data['page_size'] = $page_size;
        $admin_id && $data['admin_id'] = $admin_id;

        $result = D('order','Logic')->searchOrderList($data);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];
        //生成分页
        $url = "/admin/stat/staffStatOrderList?admin_id={$admin_id}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('admin_id', $admin_id);
        $this->assign('orderList', $orderList);
        $this->assign('userInfo', $userInfo);

        $this->loadFrame('staffStatOrderList');
    }
    /**
     * (业务员统计）管理者
     */
    public function manageStaffStatList(){
        $uid = self::$user['id'];
        $name = I('get.name');
        $group_id = I('get.group_id');
        $department_id = I('get.department_id');
        $data = array();
        $name && $data['name'] = $name;
        $group_id && $data['group_id'] = $group_id;
        $department_id && $data['department_id'] = $department_id;

        $userDepartmentIdList = D('Resource/UserDepartment', 'Service')->getUserRelationDepartmentIdList($uid);

        if(!$department_id && $userDepartmentIdList){
            $data['department_id']  = ['in',$userDepartmentIdList];
        }

        $userList = D('Stat','Logic')->staffStatList($data);
        //部门
        $departmentList = D('Resource/User','Service')->getDepartmentList(['id'=>['in',$userDepartmentIdList]]);

        $this->assign('name', $name);
        $this->assign('department_id', $department_id);
        $this->assign('group_id', $group_id);
        $this->assign('userList', $userList);
        $this->assign('departmentList', $departmentList);

        $this->loadFrame('manageStaffStatList');
    }

    /**
     * 数据导出
     */
    public function manageStaffStatExport(){
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $uid = self::$user['id'];
        $name = I('get.name');
        $department_id = I('get.department_id');
        $data = array();
        $name && $data['name'] = $name;
        $department_id && $data['department_id'] = $department_id;

        $userDepartmentIdList = D('Resource/UserDepartment', 'Service')->getUserRelationDepartmentIdList($uid);

        if(!$department_id && $userDepartmentIdList){
            $data['department_id']  = ['in',$userDepartmentIdList];
        }

        $userList = D('Stat','Logic')->staffStatList($data);

        $export_data[] = array('角色','部门','姓名','总客户数','总订单数','成单数','进行中','销售额','成本金额','其他费用','');
        //用户组
        foreach ($userList as $key=>$user){
            $export_data[] = array(
                'user_group_name' => $user['user_group_name'],
                'department_name' => $user['department_name'],
                'name' => $user['name'],
                'all_customer' => $user['all_customer'],
                'all_order' => $user['all_order'],
                'over_order' => $user['over_order'],
                'going_order' => $user['going_order'],
                'total_amount' => $user['total_amount'],
                'total_base_price' => $user['total_base_price'],
                'total_other_price' => $user['total_other_price']
            );
        }
        $xls = new \Org\Util\PhpExcel;
        $xls->addArray($export_data);
        $xls->generateXML("order_" . date("Y-m-d H:i:s"));
    }
    /**
     * 订单性情导出
     */
    public function manageExportOrderList(){
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $user = self::$user;
        $uid = $user['id'];
        $admin_id = I("get.admin_id");
        $search_time = I("get.search_time");
        $start = '';
        $end = '';
        if($search_time){
            $all_time = explode('-',$search_time);
            $start = strtotime($all_time[0]);
            $end = strtotime($all_time[1]." 23:59:59");
        }
        $params=[];
        $admin_id && $params['admin_id'] = $admin_id;
        $start && $params['start'] = $start;
        $end && $params['end'] = $end;

        $userDepartmentIdList = D('Resource/UserDepartment', 'Service')->getUserRelationDepartmentIdList($uid);

        $data = [];
        $admin_id && $data['id'] = $admin_id;
        if(!$admin_id && $userDepartmentIdList){
            $data['department_id']  = ['in',$userDepartmentIdList];
        }
        $userIds = D('Resource/User','Service')->getUserIds($data);

        $params['admin_id'] = ['in',$userIds];

        $result = D('Order', 'Logic')->manageExportOrderData($params);
        $xls = new \Org\Util\PhpExcel;
        $xls->addArray($result);
        $xls->generateXML("order_" . date("Y-m-d H:i:s"));
    }

    /**
     * 业支导出 客户
     */
    public function exportStatCustomerData(){
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $user = self::$user;
        $uid = $user['id'];
        $admin_id = I("get.admin_id");
        $search_time = I("get.search_time");
        $start = '';
        $end = '';
        if($search_time){
            $all_time = explode('-',$search_time);
            $start = strtotime($all_time[0]);
            $end = strtotime($all_time[1]." 23:59:59");
        }
        $params=[];
        $admin_id && $params['admin_id'] = $admin_id;
        $start && $params['start'] = $start;
        $end && $params['end'] = $end;

        $result = D('Order', 'Logic')->exportStatCustomerData($params);
        $xls = new \Org\Util\PhpExcel;
        $xls->addArray($result);
        $xls->generateXML("order_" . date("Y-m-d H:i:s"));
    }
}